package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ViewDebug;
import android.widget.TextView;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UDPClient extends Thread {

    private static final String TAG1 = "Send", TAG2 = "Receive";
    private String m_hostIP, localIp;
    private int localPort;
    private Handler m_handler ;

    public UDPClient(String strHostIP, Handler handler) {
        m_hostIP = strHostIP;
        m_handler = handler;
    }

    @Override
    public void run() {
        int TIMEOUT = 3000;
        int servProt = 58090;
        byte[] bytesToSend = "gufeng".getBytes();

        try {
            /*
             * 向服务器端发送数据
             */
            // 1.定义服务器的地址、端口号、数据
            InetAddress serverAddress = InetAddress.getByName(m_hostIP);

            // 2.创建DatagramSocket对象
            DatagramSocket socket = new DatagramSocket();
            socket.setSoTimeout(TIMEOUT);

            // 3.创建数据报，包含发送的数据信息
            DatagramPacket sendPacker = new DatagramPacket(bytesToSend, bytesToSend.length, serverAddress, servProt);
            // 4.向服务器端发送数据报
            socket.send(sendPacker);
            localIp = socket.getLocalAddress().toString();
            localPort = socket.getLocalPort();
            Log.i(TAG1, localIp+":"+String.valueOf(localPort)+" -> "+m_hostIP + ":58090");

            /*
             * 接收服务器端响应的数据
             */
            // 1.创建数据报，用于接收服务器端响应的数据
            byte[] bytesFromReceive = new byte[96];
            DatagramPacket receivePacket = new DatagramPacket(bytesFromReceive, bytesFromReceive.length);
            // 2.接收服务器响应的数据
            socket.receive(receivePacket);
            // 3.读取数据
            bytesToDouble(bytesFromReceive);
            //String reply = new String(bytesFromReceive, 0, receivePacket.getLength());
            //Log.i(TAG2, new String(bytesFromReceive));
            // 4.关闭资源
            socket.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void bytesToDouble(byte[] arr) {
        long[] value = new long[12];
        Message message=new Message();
        Bundle bundle=new Bundle();
        for (int j = 0; j < 12; j++) {
            for (int i = 0; i < 8; i++) {
                value[j] |= ((long) (arr[j*8+i] & 0xff)) << (8 * i);
            }
            //Log.i(TAG2, String.valueOf(Double.longBitsToDouble(value[j])));
            bundle.putDouble(String.valueOf(j), Double.longBitsToDouble(value[j]));
        }
        bundle.putDouble("recvCount", arr.length);
        Log.i(TAG2, new String(bundle.toString()));
        message.setData(bundle);//bundle传值，耗时，效率低
        message.what=1;//标志是哪个线程
        m_handler.sendMessage(message);//发送message信息
    }
}
